package com.ruoyi.faceplus.controller;

import com.arcsoft.face.FaceEngine;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.tool.ImgUtils;
import com.ruoyi.common.utils.tool.TextUtils;
import com.ruoyi.faceplus.domain.ImageFeature;
import com.ruoyi.faceplus.domain.RegisterData;
import com.ruoyi.faceplus.domain.UserFace;
import com.ruoyi.faceplus.face.FaceServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
public class FaceCompareController extends BaseController {

    @Value("${arcpro.localPhotoPath}")
    private String mLocalPhotoPath;

    @Autowired
    private FaceServer faceServer;

    /**
     * 人脸比对
     */
    @PostMapping("/face/compare")
    public AjaxResult compare(@RequestBody RegisterData registerData) {
        FaceEngine faceEngine = null;
        File tmpFile = null;
        try {
            String photo = registerData.getPhoto();
            if (photo.startsWith("http")) {
                // 网络图片转base64
                photo = ImgUtils.netImageToBase64(photo);
            }
            File filePath = new File(mLocalPhotoPath);
            if (!filePath.exists()) filePath.mkdirs();
            // 生成本地临时图片
            String tmpPhoto = mLocalPhotoPath + File.separator + System.currentTimeMillis() + "-" + TextUtils.getRandom(5) + ".jpg";
            if (!ImgUtils.generateLocalImage(photo, tmpPhoto)) {
                throw new ArithmeticException("生成文件失败");
            }

            // 检查生成文件是否存在
            tmpFile = new File(tmpPhoto);
            if (!tmpFile.exists()) {
                throw new ArithmeticException("生成文件失败");
            }
            // 获取人脸引擎
            faceEngine = faceServer.faceEnginePoll();
            boolean hasEngine = false;
            if (faceEngine != null) hasEngine = true;
            while (!hasEngine) {
                faceEngine = faceServer.faceEnginePoll();
                if (faceEngine != null) hasEngine = true;
            }

            List<ImageFeature> imageFeatures = faceServer.faceExtract(tmpFile, faceEngine);
            if (imageFeatures == null || imageFeatures.size() != 1) {
                // 注册人脸不允许带口罩
                throw new ArithmeticException("人脸特征有问题");
            }
            ImageFeature imageFeature = imageFeatures.get(0);

            UserFace userFace = faceServer.faceSearch(imageFeature.getFaceFeature(), imageFeature.getMaskInfo().getMask(), true, faceEngine);
            if (userFace == null) throw new ArithmeticException("识别失败");
            return success(userFace);
        } catch (ArithmeticException e) {
            return error(e.getMessage());
        } finally {
            if (faceEngine != null) faceServer.faceEngineOffer(faceEngine);
            if (tmpFile != null) tmpFile.delete();
        }
    }
}
